package com.yunji.base.dao.impl;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.yunji.base.bo.ChangeBindBO;
import com.yunji.base.bo.StaffAccountModifyBO;
import com.yunji.base.dao.IStaffDao;
import com.yunji.base.dto.StaffFiledDTO;
import com.yunji.base.mapper.IStaffMapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.framework.MybatisPlusHelper;
import com.yunji.common.model.domain.StaffDO;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/13 15:02
 * @description : TODO
 **/
@Repository
public class StaffDaoImpl extends BaseDaoImpl<IStaffMapper, StaffDO> implements IStaffDao {
    @Override
    public List<String> searchTableField(StaffFiledDTO param) {
        return this.baseMapper.searchTableField(param);
    }

    @Override
    public boolean changeAgent(ChangeBindBO bo) {
        return lambdaUpdate()
                .set(StaffDO::getBelongId, bo.getBelongId())
                .set(StaffDO::getAgentId, bo.getAgentId())
                .set(StaffDO::getAgentName, bo.getAgentName())
                .eq(StaffDO::getManagerId, bo.getSiteId()).update();
    }

    @Override
    public List<StaffDO> listByManagerId(Long managerId) {
        return lambdaQuery().eq(StaffDO::getManagerId, managerId).list();
    }

    @Override
    public List<StaffDO> listByAgentId(Long agentId) {
        return lambdaQuery().eq(StaffDO::getAgentId, agentId).list();
    }

    @Override
    public List<StaffDO> listByBelongId(Long belongId) {
        return lambdaQuery().eq(StaffDO::getBelongId, belongId).list();
    }


    //    UPDATE staff
//        <trim prefix="SET" suffixOverrides=",">
//            <if test="earnAmount != null"> 		earn_amount 	= 	earn_amount + #{earnAmount}, 		</if>
//            <if test="cashAmount != null"> 		cash_amount 	= 	cash_amount + #{cashAmount}, 		</if>
//            <if test="remainAmount != null"> 	remain_amount 	= 	remain_amount + #{remainAmount}, 	</if>
//            <if test="frozenAmount != null"> 	frozen_amount 	= 	frozen_amount + #{frozenAmount}, 	</if>
//            <if test="refundAmount != null"> 	refund_amount 	= 	refund_amount + #{refundAmount}, 	</if>
//            <if test="openid != null"> 			openid 			= 	#{openid}, 							</if>
//            <if test="unionid != null"> 		unionid 		= 	#{unionid}, 						</if>
//        </trim>
//    WHERE del_flag = 0 AND id =#{id}
    @Override
    public boolean updateStaffAccount(StaffAccountModifyBO build) {
        LambdaUpdateChainWrapper<StaffDO> c = lambdaUpdate();
        return c.setSql(BeanUtils.isNotNull(build.getEarnAmount()), MybatisPlusHelper.columnAddCount(StaffDO::getEarnAmount, build.getEarnAmount()))
                .setSql(BeanUtils.isNotNull(build.getCashAmount()), MybatisPlusHelper.columnAddCount(StaffDO::getCashAmount, build.getCashAmount()))
                .setSql(BeanUtils.isNotNull(build.getRemainAmount()), MybatisPlusHelper.columnAddCount(StaffDO::getRemainAmount, build.getRemainAmount()))
                .setSql(BeanUtils.isNotNull(build.getFrozenAmount()), MybatisPlusHelper.columnAddCount(StaffDO::getFrozenAmount, build.getFrozenAmount()))
                .setSql(BeanUtils.isNotNull(build.getRefundAmount()), MybatisPlusHelper.columnAddCount(StaffDO::getRefundAmount, build.getRefundAmount()))
                .eq(StaffDO::getId, build.getId()).update();
    }

    @Override
    public StaffDO getByPhone(String phone) {
        return lambdaQuery().eq(StaffDO::getStaffId, phone).one();
    }

    @Override
    public boolean existsPhone(Long excludeId, String phone) {
        return lambdaQuery().ne(excludeId != null, StaffDO::getId, excludeId).eq(StaffDO::getStaffId, phone).exists();
    }
}
